package ui.component
{
	import assets.Asset;
	
	import tool.tweener.Tweener;
	
	import events.GameEvent;
	import events.LoadedEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import tool.BitmapTool;
	
	public class EquipMenu extends Sprite
	{
		private var _type:int;
		private var _connectorID:int;
		
		private var _aniBaseBmdsURL:String = "UI/equipMenu.png";
		private var _aniBaseBmds:Array = new Array;
		private var _buttonBmds:Array = new Array;
		private var _conButBmds:Array = new Array;
		private var _aniBase:Bitmap;
		private var _aniBaseCurrentFrame:int = 0;
		
		private var _buttonsHolder:Sprite;
		private var _conButtonsHolder:Sprite;
		
		private var _equipButton:Button;
		private var _installButton:Button;
		private var _uninstallButton:Button;
		private var _sellButton:Button;
		private var _buyButton:Button;
		private var _incButton:Button;
		
		private var _buttons:Vector.<Button>;
		
		private var _timer:Timer = new Timer(300);//鼠标移出菜单多长时间后自动消失
		//////////////////////////////////////////////////////////////
		public function get connectorID():int
		{
			return _connectorID;
		}
		
		/**
		 * @param type 在仓库中选择机甲时，类型设置为1;选择武器时，类型设置为2;
		 */
		public function EquipMenu()
		{
			_conButtonsHolder = new Sprite;
			addChild(_conButtonsHolder);
			
			Asset.bitmapdata.addEventListener(LoadedEvent.BITMAPDATA_COMPLETE,animateBmdsLoaded);
			Asset.bitmapdata.load(Global.assetPath+_aniBaseBmdsURL);
			
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
		}

		public function playOut(type:int):void
		{
			_type = type;
			
			_connectorID=-1;
			
			for each(var but:Button in _buttons)but.y = 7;
			
			_buttons = new Vector.<Button>;
			_aniBaseCurrentFrame = 0;
			addEventListener(Event.ENTER_FRAME,playOutOnFrame);
		}
		
		public function displayConnectors(pidVals:Vector.<int>):void
		{
			if(pidVals.length>0)
			{
				_conButtonsHolder.x = _installButton.x+8;
				_conButtonsHolder.y = _installButton.y;
				var num:int = _conButtonsHolder.numChildren;
				for(var p:int = 0;p<num;p++)_conButtonsHolder.removeChild(_conButtonsHolder.getChildAt(0));
				
				for(var i:int=0;i<pidVals.length;i++)
				{
					var btn:Button;
					
					if(pidVals[i]==0)
					{
						btn = new Button(_conButBmds[1],_conButBmds[2],_conButBmds[3],callConnect);
					}else
					{
						btn = new Button(_conButBmds[0],_conButBmds[0],_conButBmds[0]);
					}
					_conButtonsHolder.addChild(btn);
					
					var tarY:int = (i-1)*btn.height;
					Tweener.addTween(btn,{y:tarY,time:0.4});
				}
				
				Tweener.addTween(_conButtonsHolder,{y:-0.5*(_conButtonsHolder.numChildren-1)*20,time:0.4});
				Tweener.addTween(_conButtonsHolder,{x:96,time:0.4});
			}
		}
		
		private function playOutOnFrame(e:Event):void
		{
			if(_aniBaseBmds.length==0 || _buttonBmds.length==0)return;
			
			_aniBaseCurrentFrame++;
			if(_aniBaseCurrentFrame>_aniBaseBmds.length-1)
			{
				removeEventListener(Event.ENTER_FRAME,playOutOnFrame);
				
				if(_type==0)
				{
					_buttons.push(_equipButton);
					_buttons.push(_incButton);
					_buttons.push(_sellButton);
				}else if(_type==1)
				{
					_buttons.push(_installButton);
					_buttons.push(_uninstallButton);
					_buttons.push(_incButton);
					_buttons.push(_sellButton);
				}
				
				for(var i:int=0;i<_buttons.length;i++)
				{
					var btn:Button = _buttons[i];
					btn.y = 7;
					var tarY:int = -(i+1)*btn.height;
					
					Tweener.addTween(btn,{y:tarY,time:0.4});
				}
				
				return;
			}
			
			_aniBase.bitmapData = _aniBaseBmds[_aniBaseCurrentFrame];
		}
		
		private function animateBmdsLoaded(e:LoadedEvent):void
		{
			if(e.URL==(Global.assetPath+_aniBaseBmdsURL))
			{
				Asset.bitmapdata.removeEventListener(LoadedEvent.BITMAPDATA_COMPLETE,animateBmdsLoaded);
				_aniBaseBmds = BitmapTool.cutImageWithSameSize(Asset.resBitmapDatas[Global.assetPath+_aniBaseBmdsURL],1,6,85,84,83,7);
				_buttonBmds = BitmapTool.cutImageWithSameSize(Asset.resBitmapDatas[Global.assetPath+_aniBaseBmdsURL],1,18,0,0,85,21);
				_conButBmds = BitmapTool.cutImageWithSameSize(Asset.resBitmapDatas[Global.assetPath+_aniBaseBmdsURL],1,4,85,0,60,21);
				
				//Asset.disposeBitmapData(Asset.assetPath+_aniBaseBmdsURL);
				setup();
			}
		}
		
		private function setup():void
		{
			_aniBase = new Bitmap(_aniBaseBmds[_aniBaseCurrentFrame]);
			addChild(_aniBase);
			
			_buttonsHolder = new Sprite;
			addChild(_buttonsHolder);
			
			var maskData:BitmapData = new BitmapData(155,105,false,0);
			var mask:Bitmap = new Bitmap(maskData);
			_buttonsHolder.addChild(mask);
			mask.y = -103;
			_buttonsHolder.mask = mask;
			
			_equipButton = new Button(_buttonBmds[4],_buttonBmds[10],_buttonBmds[16],callEquip);
			_installButton = new Button(_buttonBmds[3],_buttonBmds[9],_buttonBmds[15],callInstall);
			_uninstallButton = new Button(_buttonBmds[2],_buttonBmds[8],_buttonBmds[14],callUninstall);
			_sellButton = new Button(_buttonBmds[0],_buttonBmds[6],_buttonBmds[12],callSell);
			_buyButton = new Button(_buttonBmds[1],_buttonBmds[7],_buttonBmds[13],callBuy);
			_incButton = new Button(_buttonBmds[5],_buttonBmds[11],_buttonBmds[17],callIncrease);
			
			for each(var but:Button in [_equipButton,_installButton,_uninstallButton,_sellButton,_buyButton,_incButton])
			{
				but.x = 15;
				but.y = 7;
			}
			
			_buttonsHolder.addChild(_equipButton);
			_buttonsHolder.addChild(_installButton);
			_buttonsHolder.addChild(_uninstallButton);
			_buttonsHolder.addChild(_sellButton);
			_buttonsHolder.addChild(_buyButton);
			_buttonsHolder.addChild(_incButton);
		}
		
		
		private function onMouseOut(e:MouseEvent):void
		{
			_timer.start();
			_timer.addEventListener(TimerEvent.TIMER,removeThis);
		}
		
		private function onMouseOver(e:MouseEvent):void
		{
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER,removeThis);
		}
		
		private function removeThis(e:TimerEvent):void
		{
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER,removeThis);
			
			_conButtonsHolder.x = _installButton.x+8;
			_conButtonsHolder.y = _installButton.y;
			var num:int = _conButtonsHolder.numChildren;
			for(var p:int = 0;p<num;p++)_conButtonsHolder.removeChild(_conButtonsHolder.getChildAt(0));
			
			try{parent.removeChild(this);}catch(e:Error) {}
		}
		
		private function callEquip():void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_EQUIP));
		}
		private function callInstall():void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_INSTALL));
		}
		private function callUninstall():void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_UNINSTALL));
		}
		private function callConnect(e:MouseEvent):void
		{
			var btn:Button = e.currentTarget as Button;
			_connectorID = _conButtonsHolder.getChildIndex(btn);
			
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_CONNECT));
		}
		private function callIncrease():void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_INCREASE));
		}
		private function callSell():void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_SELL));
		}
		private function callBuy():void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_BUY));
		}
		
		
	}
}